package com.scoy.honeymei.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.scoy.honeymei.R;
import com.scoy.honeymei.bean.CouponBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ClassName: NormalAdapter$
 * @Description:
 * @Author: GaoZhan
 * @CreateDate: 2019/8/26/0026$ 下午 17:03$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/26/0026$ 下午 17:03$
 * @UpdateRemark: 更新内容
 */
public class CheckTicketAdapter extends RecyclerView.Adapter {
    private Context context;
    private ArrayList<CouponBean> datalist;
    private boolean onBind;
    private Map<Integer, Boolean> map = new HashMap<>();
    private int checkedPosition = -1;

    public CheckTicketAdapter(Context context, ArrayList<CouponBean> datalist) {
        this.context = context;
        this.datalist = datalist;
    }

    public interface OnOneClick {
        void oneClick(int pos);
    }

    OnOneClick onOneClick;

    public void setOnOneClick(OnOneClick onOneClick) {
        this.onOneClick = onOneClick;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_ticket_check, parent, false);
        return new CTHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        CTHolder ctHolder = (CTHolder) holder;
        CouponBean couponBean = datalist.get(position);
        if ("film".equals(couponBean.getType())) {
            ctHolder.ictaTimeTv.setVisibility(View.GONE);
            if (position != 0) {
                ctHolder.ictaTimeTv.setVisibility(View.VISIBLE);
                ctHolder.ictaNameTv.setText(couponBean.getMoney() + "元 观影劵");
                ctHolder.ictaTimeTv.setText("有效日期：" + couponBean.getEndtime());
            } else {
                ctHolder.ictaNameTv.setText("不使用优惠券");
            }

        } else {
            ctHolder.ictaNameTv.setText(couponBean.getType_name() + "  满" + couponBean.getCondition() + "元优惠" + couponBean.getMoney() + "元");
            ctHolder.ictaTimeTv.setText("有效日期：" + couponBean.getEndtime());
        }


        ctHolder.ictaCheckIv.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked == true) {
                    map.clear();
                    map.put(position, true);
                    checkedPosition = position;
                } else {
                    map.remove(position);
                    if (map.size() == 0) {
                        checkedPosition = -1; //-1 代表一个都未选择
                    }
                }
                if (!onBind) {
                    notifyDataSetChanged();
                }
            }
        });
        onBind = true;
        if (map != null && map.containsKey(position)) {
            ctHolder.ictaCheckIv.setChecked(true);
        } else {
            ctHolder.ictaCheckIv.setChecked(false);
        }
        onBind = false;

       /* ctHolder.ictaCheckIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onOneClick != null) {
                    onOneClick.oneClick(position);
                }
            }
        });*/

    }

    //得到当前选中的位置
    public int getCheckedPosition() {
        return checkedPosition;
    }

    public void setCheckedPosition(int checkedPosition) {
        map.put(checkedPosition, true);
        notifyItemChanged(checkedPosition);
    }


    @Override
    public int getItemCount() {
        return datalist == null ? 0 : datalist.size();
    }

    class CTHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.icta_name_tv)
        TextView ictaNameTv;
        @BindView(R.id.icta_time_tv)
        TextView ictaTimeTv;
        @BindView(R.id.icta_check_iv)
        CheckBox ictaCheckIv;
        @BindView(R.id.item_income)
        ConstraintLayout itemIncome;

        CTHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
